package nameless.aistudy.global.advice;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import nameless.aistudy.common.exception.BusinessException;
import nameless.aistudy.common.result.Result;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class BusinessExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<Result<String>> handleBusinessException(HttpServletRequest request, BusinessException e) {
        Result<String> result = Result.fail(e.getStatus(), e.getMessage());
        log.warn(
                "[业务异常] {} {}\nResponse: {}",
                request.getMethod(),
                request.getRequestURI(),
                result
        );
        return ResponseEntity.status(result.getCode()).body(result);
    }

}
